const Controller = require("egg").Controller;

class GradeController extends Controller {
    async create() {
        // 解析请求参数
        const { name, topic, room_id } = this.ctx.request.body;
        // 根据教室id，去数据库中查找，是否存在这个教室
        const room = await this.service.room.find(room_id);
        if (!room) {
            this.ctx.body = {
                code: 404,
                message: "该教室不存在",
            };
            return;
        }
        // 保存到数据库中
        const res = await this.service.grade.add(name, topic, room_id);
        if (res.affectedRows === 1) {
            this.ctx.body = {
                code: 201,
                message: "添加成功",
            };
        }
    }

    async index() {
        const res = await this.service.grade.readList();
        this.ctx.body = {
            code: 200,
            data: res,
            message: "请求成功",
        };
    }

    async update() {}

    async delete() {}
}

module.exports = GradeController;
